*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   	 Local Data 
*
*===============================================================================


********************************************************************************
*								Figure IX									   *
********************************************************************************

*create program for binscatters - with reference lines
capture program drop twbinscatter		
program define twbinscatter

	args yvar xvar ytitle xtitle ylabel xlabel marker_treat marker_control patt_treat patt_control slope_ref
	
reg   `yvar' `xvar' if personalinfo==1
	  local cons_m = string(round(_b[_cons],0.001),"%7.3f")
      local beta_m = string(round(_b[`xvar'],0.001),"%7.3f") 
      local se_m = string(round(_se[`xvar'],0.001),"%7.3f") 
	  local samp_m = e(N)

reg   `yvar' `xvar' if personalinfo==0
	  local cons_f = string(round(_b[_cons],0.001),"%7.3f")
      local beta_f = string(round(_b[`xvar'],0.001),"%7.3f") 
      local se_f = string(round(_se[`xvar'],0.001),"%7.3f") 
	  local samp_f = e(N)  	  
	  
preserve
qui binscatter `yvar' `xvar' if personalinfo==1, savedata("$temp/binned_`yvar'_treated") replace
restore

preserve
qui binscatter `yvar' `xvar' if personalinfo==0, savedata("$temp/binned_`yvar'_control") replace
restore

preserve
import delimited "$temp/binned_`yvar'_treated.csv", clear
gen personalinfo=1
summ `xvar'
local min_m : di %7.3g `r(min)'
local max_m : di %7.3g `r(max)'
tempfile treated
save `treated'

import delimited "$temp/binned_`yvar'_control.csv", clear

gen personalinfo=0
summ `xvar'
local min_f : di %7.3g `r(min)'
local max_f : di %7.3g `r(max)'
tempfile control
save `control'

use `treated', clear
append using `control'

global slope_ref `slope_ref'

if `slope_ref'==1{
	
twoway (connect `yvar' `xvar' if `yvar'==1.8, lc(navy) mc(navy) msymbol(O) lpattern(solid)) /// 
(connect `yvar' `xvar' if `xvar'==1.8, lc(maroon) mc(maroon) msymbol(Th) lpattern(dash)) /// 
(function y = x, range(-0.6 0.6) color(black%50)) ///
(function y = 0, range(-0.6 0.6) color(black%50)) ///
(function 0*x^2+`beta_m'*x+`cons_m', range(`min_m' `max_m') lp(`patt_treat') lc(navy)) ///
(scatter `yvar' `xvar' if personalinfo == 1 , mc(navy) m(`marker_treat')) ///
(function 0*x^2+`beta_f'*x+`cons_f', range(`min_f' `max_f') lp(`patt_control') lc(maroon)) ///
(scatter `yvar' `xvar' if personalinfo == 0 , mc(maroon) m(`marker_control')) , ///
ytitle("`ytitle'") xtitle("`xtitle'") xlabel(`xlabel', grid)  ylabel(`ylabel', grid)  legend(order(1 "Treatment Slope: `beta_m' (SE `se_m')" 2 "Control Slope: `beta_f' (SE `se_f')") size(medsmall) rows(2) region(c(none)) symysize(5) margin(vsmall) bmargin(6) pos(10) ring(0)) 	
addplot: scatteri -0.275 -0.2 "No Updating: Slope = 1", msymbol(i) mlabpos(0) mlabcolor(black) mlabangle(35) legend(order(1 "Treatment Slope: `beta_m' (SE `se_m')" 2 "Control Slope: `beta_f' (SE `se_f')") size(medsmall) rows(2) region(c(none)) symysize(5) margin(vsmall) bmargin(6) pos(10) ring(0)) 	xlabel(`xlabel', grid)  ylabel(`ylabel', grid)
addplot: scatteri 0.05 0.4 "Full Updating: Slope = 0", msymbol(i) mlabpos(0) mlabcolor(black) legend(order(1 "Treatment Slope: `beta_m' (SE `se_m')" 2 "Control Slope: `beta_f' (SE `se_f')") size(medsmall) rows(2) region(c(none)) symysize(5) margin(vsmall) bmargin(6) pos(10) ring(0)) xlabel(`xlabel', grid)  ylabel(`ylabel', grid)
	
}

erase "$temp/binned_`yvar'_treated.csv"
erase "$temp/binned_`yvar'_treated.do"
erase "$temp/binned_`yvar'_control.csv"
erase "$temp/binned_`yvar'_control.do"

end

*create program for binscatters
capture program drop twbinscatter_basic	
program define twbinscatter_basic

	args yvar xvar ytitle xtitle ylabel xlabel marker_treat marker_control patt_treat patt_control connected_place
	
reg   `yvar' `xvar' if personalinfo==1
	  local cons_m = string(round(_b[_cons],0.001),"%7.3f")
      local beta_m = string(round(_b[`xvar'],0.001),"%7.3f") 
      local se_m = string(round(_se[`xvar'],0.001),"%7.3f") 
	  local samp_m = e(N)

reg   `yvar' `xvar' if personalinfo==0
	  local cons_f = string(round(_b[_cons],0.001),"%7.3f")
      local beta_f = string(round(_b[`xvar'],0.001),"%7.3f") 
      local se_f = string(round(_se[`xvar'],0.001),"%7.3f") 
	  local samp_f = e(N)
	  
preserve
qui binscatter `yvar' `xvar' if personalinfo==1, savedata("$temp/binned_`yvar'_treated") replace
restore

preserve
qui binscatter `yvar' `xvar' if personalinfo==0, savedata("$temp/binned_`yvar'_control") replace
restore

preserve
import delimited "$temp/binned_`yvar'_treated.csv", clear
gen personalinfo=1
summ `xvar'
local min_m : di %7.3g `r(min)'
local max_m : di %7.3g `r(max)'
tempfile treated
save `treated'

import delimited "$temp/binned_`yvar'_control.csv", clear

gen personalinfo=0
summ `xvar'
local min_f : di %7.3g `r(min)'
local max_f : di %7.3g `r(max)'
tempfile control
save `control'

use `treated', clear
append using `control'


twoway (connect `yvar' `xvar' if `yvar'==`connected_place', lc(navy) mc(navy) msymbol(O) lpattern(solid)) /// 
(connect `yvar' `xvar' if `xvar'==`connected_place', lc(maroon) mc(maroon) msymbol(Th) lpattern(dash)) /// 
(function 0*x^2+`beta_m'*x+`cons_m', range(`min_m' `max_m') lp(`patt_treat') lc(navy)) ///
(scatter `yvar' `xvar' if personalinfo == 1 , mc(navy) m(`marker_treat')) ///
(function 0*x^2+`beta_f'*x+`cons_f', range(`min_f' `max_f') lp(`patt_control') lc(maroon)) ///
(scatter `yvar' `xvar' if personalinfo == 0 , mc(maroon) m(`marker_control')) , ///
ytitle("`ytitle'") xtitle("`xtitle'") xlabel(`xlabel', grid)  ylabel(`ylabel', grid)  legend(lab(1 "Treatment Slope: `beta_m' (SE `se_m')") lab(2 "Control Slope: `beta_f' (SE `se_f')") size(medsmall) rows(2) region(c(none)) symysize(5) margin(vsmall) bmargin(6) order(1 2) pos(10) ring(0)) 
	

erase "$temp/binned_`yvar'_treated.csv"
erase "$temp/binned_`yvar'_treated.do"
erase "$temp/binned_`yvar'_control.csv"
erase "$temp/binned_`yvar'_control.do"

end

********************************************************************************
*								Figure IXa									   *
********************************************************************************

use "$temp/experiment1.dta", clear

cap drop truthpercentgap 
cap drop beliefpercentgap 

gen truthpercentgap = ((meanwage_raw2 - pretaxwageearning)/pretaxwageearning)
gen beliefpercentgap = ((point_more - pretaxwageearning) / pretaxwageearning)
	
gen beliefpercentgap_post = ((belief_post-pretaxwageearning)/pretaxwageearning)
	
* levels pre-post
winsor point_more, gen(point_more_w2) p(0.02)
winsor belief_post, gen(belief_post_w2) p(0.02)

foreach var in point_more belief_post meanwage_raw2 {
	gen `var'_ownpct = (`var'/pretaxwageearning)
	winsor `var'_ownpct, p(0.02) gen(`var'_w2_ownpct)
	drop `var'_ownpct
}

rename meanwage_raw2_w2_ownpct meanwage_raw2_ownpct
	
forvalues n = 0/1 {
	reg bias_belief_post_pct_w2 bias_belief_pct_w2 if personalinfo==`n'
	local beta`n' : di %5.3g _b[bias_belief_pct_w2]
	local se`n' : di %5.3g _se[bias_belief_pct_w2]
}
	
twbinscatter bias_belief_post_pct_w2 bias_belief_pct_w2 "Post-Treatment Estimation Error (Similar Worker Wage)" "Pre-Treatment Estimation Error (Similar Worker Wage)" -0.60(0.2)0.60 -0.6(0.2)0.6 O Th line dash 1
graph export "$fig/FigureIXa.pdf", replace

********************************************************************************
*								Figure IXb									   *
********************************************************************************

use "$temp/experiment1.dta", clear

cap drop truthpercentgap
cap drop beliefpercentgap 

gen truthpercentgap = ((meanwage_raw2 - pretaxwageearning)/pretaxwageearning)
gen beliefpercentgap = ((point_more - pretaxwageearning) / pretaxwageearning)
	
gen beliefpercentgap_post = ((belief_post-pretaxwageearning)/pretaxwageearning)
	
* levels pre-post
winsor point_more, gen(point_more_w2) p(0.02)
winsor belief_post, gen(belief_post_w2) p(0.02)

foreach var in point_more belief_post meanwage_raw2 {
	gen `var'_ownpct = (`var'/pretaxwageearning)
	winsor `var'_ownpct, p(0.02) gen(`var'_w2_ownpct)
	drop `var'_ownpct
	
}

rename meanwage_raw2_w2_ownpct meanwage_raw2_ownpct
	
forvalues n = 0/1 {
	reg posttreatment_wagechange_pct_w2 bias_belief_pct_w2 if personalinfo==`n'
	local beta`n' : di %5.3g _b[bias_belief_pct_w2]
	local se`n' : di %5.3g _se[bias_belief_pct_w2]
}

twbinscatter_basic posttreatment_wagechange_pct_w2 bias_belief_pct_w2 "Post-Treatment Belief: Own Wage Change" "Pre-Treatment Estimation Error (Similar Worker Wage)" -0.30(0.15)0.60 -0.6(0.2)0.6 O Th line dash 0
graph export "$fig/FigureIXb.pdf", replace
